﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;

namespace UI.Admin.Content.ModelContent
{
    /// <summary>
    /// AjaxAction1 的摘要说明
    /// </summary>
    public class AjaxAction : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            // 验证
            //string auth = Utility.Authorization.Check(HttpContext.Current.Session["role"], "ContentAdmin", context.Request["ColumnID"]);
           // if (auth != "ACCEPT")
           // {
           //     return;
            //}
        
            context.Response.ContentType = "text/plain";

            string dowhat = context.Request["dowhat"];
            if (dowhat == null) dowhat = "";

            // 操作用户及IP
            string UserName = ((Models.User)(HttpContext.Current.Session["user"])).Name;
            string UserIP = context.Request.ServerVariables["LOCAl_ADDR"];

            #region 添加单页模型的内容
            if (dowhat == "add_content")
            {
                string ColumnID = context.Request["ColumnID"];

                //发布者和最后编辑者
                string FirstEditor = UserName;
                string LastEditor = UserName;

                string Title = context.Request["Title"];
                string Content = context.Request["Content"];
                string UpdateDate = DateTime.Now.ToString();

                string Name = BLL.Find.FindOneColumn(ColumnID).Name;

                if (BLL.Create.InsertContent(ColumnID, FirstEditor, LastEditor, Title, Content, UpdateDate) > 0)
                {
                    // Log
                    BLL.Create.CreateLogRecord(UserName, UserIP, "添加单页模型栏目【" + Name + "】" + Title + "内容");
                    context.Response.Write("success");
                }
                else
                {
                    // Log
                    context.Response.Write("faild");
                }
                context.Response.End();
                return;
            }
            #endregion

            #region 更新单页模型的内容
            if (dowhat == "Update_content")
            {
                string ID = context.Request["ID"];
                string ColumnID = context.Request["ColumnID"];

                //修改最后编辑者
                string LastEditor = UserName;

                string Title = context.Request["Title"];
                string Content = context.Request["Content"];
                string UpdateDate = DateTime.Now.ToString();

                string Name = BLL.Find.FindOneColumn(ColumnID).Name;

                if (BLL.Update.UpdateContent(ID, ColumnID, LastEditor, Title, Content, UpdateDate) > 0)
                {
                    // Log
                    BLL.Create.CreateLogRecord(UserName, UserIP, "更新单页模型栏目【" + Name + "】" + Title + "内容");
                    context.Response.Write("success");
                }
                else
                {
                    // Log
                    context.Response.Write("faild");
                }

                context.Response.End();
                return;
            }
            #endregion
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}